package com.xzy.generic;

import lombok.Getter;
import lombok.ToString;
import org.springframework.context.ApplicationEvent;
import org.springframework.core.ResolvableType;
import org.springframework.core.ResolvableTypeProvider;

/**
 * 自定义事件
 *
 * @author xzy.xiao
 * @date 2022/10/8  18:57
 */
@Getter
@ToString
public class GenericEvent<T> extends ApplicationEvent implements ResolvableTypeProvider {

    // ==================== field ====================

    /**
     * 事件类型
     */
    private final EventType eventType;

    /**
     * 事件数据
     */
    private final T eventData;


    // ==================== constructor ====================


    public GenericEvent(Object source, EventType eventType, T eventData) {
        super(source);
        this.eventType = eventType;
        this.eventData = eventData;
    }

    /**
     * Return the {@link ResolvableType} describing this instance
     * (or {@code null} if some sort of default should be applied instead).
     */
    @Override
    public ResolvableType getResolvableType() {
        return ResolvableType.forClassWithGenerics(
                this.getClass(),
                ResolvableType.forClass(this.eventData.getClass())
        );
    }

    public enum EventType {
        /**
         * 新增
         */
        INSERT,
        /**
         * 删除
         */
        DELETE,
        /**
         * 编辑
         */
        UPDATE
    }
}
